﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DIProject.Data
{
    public class Refusals
    {
        public static DataSets.Util.AbsenceLinesInsertDataTable AddRefusal(Int32 GoodsId,Int32 Qntty,string Note)
        {
            DateTime DocDate = DateTime.Now;
            //вставляем null номер создастся в базе
            string DocNumber = null;
            ///описание
            string DocNote = null;
            DateTime MD = DateTime.Now;
            string MW = FirstForm.Login;
            DataSets.Util.AbsenceLinesInsertDataTable result;
            DataSets.Util.AbsenceDocInsertDataTable tmpDoc;
            DataSets.Util.AbsenceDocInsertRow newDocRow;
            try
            {
                DataSets.UtilTableAdapters.AbsenceDocInsertTableAdapter docAdapter = new DIProject.DataSets.UtilTableAdapters.AbsenceDocInsertTableAdapter();
                tmpDoc = docAdapter.GetData(DocDate, DocNumber, DocNote, MD, MW);
                if (tmpDoc == null)
                    throw new Util.DIException("Невозможно создать отказ");
                if (tmpDoc.Count != 1)
                    throw new Util.DIException("Невозможно создать отказ");
                newDocRow = tmpDoc.Rows[0] as DataSets.Util.AbsenceDocInsertRow;
                if (newDocRow == null)
                    throw new Util.DIException("Невозможно создать отказ");
                DataSets.UtilTableAdapters.AbsenceLinesInsertTableAdapter lineAdapter = new DIProject.DataSets.UtilTableAdapters.AbsenceLinesInsertTableAdapter();
                result = lineAdapter.GetData(newDocRow.Id, GoodsId, Qntty, Note, MD, MW);
                return result;
            }
            catch (System.Data.SqlClient.SqlException se)
            {
                throw new Util.DIException("Невозможно создать отказ", se);
            }
            catch (Exception ee)
            {
                throw new Util.DIException("Невозможно создать отказ", ee);
            }
        }
    }
}
